﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using jnxc.DBUtility;//Please add references
namespace jnxc.DAL
{
	/// <summary>
	/// 数据访问类:stock
	/// </summary>
	public partial class stock
	{
		public stock()
		{}
		#region  BasicMethod

		/// <summary>
		/// 得到最大ID
		/// </summary>
		public int GetMaxId()
		{
		return DbHelperSQL.GetMaxID("stockid", "stock"); 
		}

		/// <summary>
		/// 是否存在该记录
		/// </summary>
		public bool Exists(int stockid)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) from stock");
			strSql.Append(" where stockid=@stockid");
			SqlParameter[] parameters = {
					new SqlParameter("@stockid", SqlDbType.Int,4)
			};
			parameters[0].Value = stockid;

			return DbHelperSQL.Exists(strSql.ToString(),parameters);
		}
        public bool Exists(int uid,int mid)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from stock");
            strSql.Append(" where uid=@uid and mid=@mid");
            SqlParameter[] parameters = {
					new SqlParameter("@uid", SqlDbType.Int,4),
                    new SqlParameter("@mid", SqlDbType.Int,4)
			};
            parameters[0].Value = uid;
            parameters[1].Value = mid;

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }

        public int Add(int uid,int mid,decimal snums,int type)
        {
            int i = 0;
            if (Exists(uid, mid))
            {
                if (Update(uid, mid, snums, type))
                    i = 1;

            }
            else
            {
               i= Add(uid,mid,snums);
               if (i != 0)
                   i = 1;

            }
            return i;
        }
        public int Add(int uid,int mid,decimal snums)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into stock(");
            strSql.Append("stime,uid,mid,snums)");
            strSql.Append(" values (");
            strSql.Append("@stime,@uid,@mid,@snums)");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {
					new SqlParameter("@stime", SqlDbType.DateTime),
					new SqlParameter("@uid", SqlDbType.Int,4),
					new SqlParameter("@mid", SqlDbType.Int,4),
					new SqlParameter("@snums", SqlDbType.Float,8)};
            parameters[0].Value = DateTime.Now.ToLocalTime();
            parameters[1].Value = uid;
            parameters[2].Value = mid;
            parameters[3].Value = snums;

            object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
		/// <summary>
		/// 增加一条数据
		/// </summary>
		public int Add(jnxc.Model.stock model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("insert into stock(");
			strSql.Append("stime,uid,mid,snums)");
			strSql.Append(" values (");
			strSql.Append("@stime,@uid,@mid,@snums)");
			strSql.Append(";select @@IDENTITY");
			SqlParameter[] parameters = {
					new SqlParameter("@stime", SqlDbType.DateTime),
					new SqlParameter("@uid", SqlDbType.Int,4),
					new SqlParameter("@mid", SqlDbType.Int,4),
					new SqlParameter("@snums", SqlDbType.Float,8)};
			parameters[0].Value = model.stime;
			parameters[1].Value = model.uid;
			parameters[2].Value = model.mid;
			parameters[3].Value = model.snums;

			object obj = DbHelperSQL.GetSingle(strSql.ToString(),parameters);
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}
        public bool Update(int uid,int mid,decimal snum,int type)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update stock set ");
            strSql.Append("stime=@stime,");            
            if(type==0)
            strSql.Append("snums=@snums+snums");
            else
                strSql.Append("snums=snums-@snums");

            strSql.Append(" where uid=@uid and mid=@mid");
            
            SqlParameter[] parameters = {
					new SqlParameter("@stime", SqlDbType.DateTime),					
					new SqlParameter("@snums", SqlDbType.Float,8),
					new SqlParameter("@uid", SqlDbType.Int,4),
                    new SqlParameter("@mid", SqlDbType.Int,4)};
            parameters[0].Value = DateTime.Now.ToLocalTime();
            parameters[1].Value = snum;
            parameters[2].Value = uid;
            parameters[3].Value = mid;           

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
		/// <summary>
		/// 更新一条数据
		/// </summary>
		public bool Update(jnxc.Model.stock model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("update stock set ");
			strSql.Append("stime=@stime,");
			strSql.Append("uid=@uid,");
			strSql.Append("mid=@mid,");
			strSql.Append("snums=@snums");
			strSql.Append(" where stockid=@stockid");
			SqlParameter[] parameters = {
					new SqlParameter("@stime", SqlDbType.DateTime),
					new SqlParameter("@uid", SqlDbType.Int,4),
					new SqlParameter("@mid", SqlDbType.Int,4),
					new SqlParameter("@snums", SqlDbType.Float,8),
					new SqlParameter("@stockid", SqlDbType.Int,4)};
			parameters[0].Value = model.stime;
			parameters[1].Value = model.uid;
			parameters[2].Value = model.mid;
			parameters[3].Value = model.snums;
			parameters[4].Value = model.stockid;

			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}

		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool Delete(int stockid)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from stock ");
			strSql.Append(" where stockid=@stockid");
			SqlParameter[] parameters = {
					new SqlParameter("@stockid", SqlDbType.Int,4)
			};
			parameters[0].Value = stockid;

			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		/// <summary>
		/// 批量删除数据
		/// </summary>
		public bool DeleteList(string stockidlist )
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from stock ");
			strSql.Append(" where stockid in ("+stockidlist + ")  ");
			int rows=DbHelperSQL.ExecuteSql(strSql.ToString());
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public jnxc.Model.stock GetModel(int stockid)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select  top 1 stockid,stime,uid,mid,snums from stock ");
			strSql.Append(" where stockid=@stockid");
			SqlParameter[] parameters = {
					new SqlParameter("@stockid", SqlDbType.Int,4)
			};
			parameters[0].Value = stockid;

			jnxc.Model.stock model=new jnxc.Model.stock();
			DataSet ds=DbHelperSQL.Query(strSql.ToString(),parameters);
			if(ds.Tables[0].Rows.Count>0)
			{
				return DataRowToModel(ds.Tables[0].Rows[0]);
			}
			else
			{
				return null;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public jnxc.Model.stock DataRowToModel(DataRow row)
		{
			jnxc.Model.stock model=new jnxc.Model.stock();
			if (row != null)
			{
				if(row["stockid"]!=null && row["stockid"].ToString()!="")
				{
					model.stockid=int.Parse(row["stockid"].ToString());
				}
				if(row["stime"]!=null && row["stime"].ToString()!="")
				{
					model.stime=DateTime.Parse(row["stime"].ToString());
				}
				if(row["uid"]!=null && row["uid"].ToString()!="")
				{
					model.uid=int.Parse(row["uid"].ToString());
				}
				if(row["mid"]!=null && row["mid"].ToString()!="")
				{
					model.mid=int.Parse(row["mid"].ToString());
				}
				if(row["snums"]!=null && row["snums"].ToString()!="")
				{
					model.snums=decimal.Parse(row["snums"].ToString());
				}
			}
			return model;
		}

		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetList(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select stockid,stime,uid,mid,snums ");
			strSql.Append(" FROM stock ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			return DbHelperSQL.Query(strSql.ToString());
		}

		/// <summary>
		/// 获得前几行数据
		/// </summary>
		public DataSet GetList(int Top,string strWhere,string filedOrder)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select ");
			if(Top>0)
			{
				strSql.Append(" top "+Top.ToString());
			}
			strSql.Append(" stockid,stime,uid,mid,snums ");
			strSql.Append(" FROM stock ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			strSql.Append(" order by " + filedOrder);
			return DbHelperSQL.Query(strSql.ToString());
		}

		/// <summary>
		/// 获取记录总数
		/// </summary>
		public int GetRecordCount(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) FROM Vstock ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			object obj = DbHelperSQL.GetSingle(strSql.ToString());
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}
        public int GetRecordCount(string strWhere,int level)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) FROM Vstock ");
            if (level == 3)
                strSql.Append("select count(1) FROM Vmystockpro ");
            else if (level == 2)
                strSql.Append("select count(1) FROM Vmystockcity ");
            else if (level == 1)
                strSql.Append("select count(1) FROM Vmystockcounty ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            object obj = DbHelperSQL.GetSingle(strSql.ToString());
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("SELECT * FROM ( ");
			strSql.Append(" SELECT ROW_NUMBER() OVER (");
			if (!string.IsNullOrEmpty(orderby.Trim()))
			{
				strSql.Append("order by T." + orderby );
			}
			else
			{
				strSql.Append("order by T.stockid desc");
			}
			strSql.Append(")AS Row, T.*  from Vstock T ");
			if (!string.IsNullOrEmpty(strWhere.Trim()))
			{
				strSql.Append(" WHERE " + strWhere);
			}
			strSql.Append(" ) TT");
			strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
			return DbHelperSQL.Query(strSql.ToString());
		}
        public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex,int level)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT * FROM ( ");
            strSql.Append(" SELECT ROW_NUMBER() OVER (");
            if (!string.IsNullOrEmpty(orderby.Trim()))
            {
                strSql.Append("order by T." + orderby);
            }
            else
            {
                strSql.Append("order by T.stockid desc");
            }            
            strSql.Append(")AS Row, T.*  from Vmystockpro T ");           
            if (!string.IsNullOrEmpty(strWhere.Trim()))
            {
                strSql.Append(" WHERE " + strWhere);
            }
            strSql.Append(" ) TT");
            strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
            return DbHelperSQL.Query(strSql.ToString());
        }

		/*
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetList(int PageSize,int PageIndex,string strWhere)
		{
			SqlParameter[] parameters = {
					new SqlParameter("@tblName", SqlDbType.VarChar, 255),
					new SqlParameter("@fldName", SqlDbType.VarChar, 255),
					new SqlParameter("@PageSize", SqlDbType.Int),
					new SqlParameter("@PageIndex", SqlDbType.Int),
					new SqlParameter("@IsReCount", SqlDbType.Bit),
					new SqlParameter("@OrderType", SqlDbType.Bit),
					new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
					};
			parameters[0].Value = "stock";
			parameters[1].Value = "stockid";
			parameters[2].Value = PageSize;
			parameters[3].Value = PageIndex;
			parameters[4].Value = 0;
			parameters[5].Value = 0;
			parameters[6].Value = strWhere;	
			return DbHelperSQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
		}*/

		#endregion  BasicMethod
		#region  ExtensionMethod

		#endregion  ExtensionMethod
	}
}

